//
//  TTRoundProgressView.m
//  Tint
//
//  Created by Kevin Wu on 2/27/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTRoundProgressView.h"
#import "TTCore.h"
#import "TTAdditions.h"



@implementation TTRoundProgressView


#pragma mark - Accessors

@synthesize borderWidth = _borderWidth;

- (void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    [self setNeedsDisplay];
}


@synthesize borderColor = _borderColor;

- (void)setBorderColor:(UIColor *)borderColor {
    TTRetain(borderColor, _borderColor);
    [self setNeedsDisplay];
}


@synthesize backgroundColor = _backgroundColor;

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    TTRetain(backgroundColor, _backgroundColor);
    [self setNeedsDisplay];
}


@synthesize fillColor = _fillColor;

- (void)setFillColor:(UIColor *)fillColor {
    TTRetain(fillColor, _fillColor);
    [self setNeedsDisplay];
}



#pragma mark - Memory

- (id)init {
    self = [super initWithFrame:TTRectWithW(37.0f)];
    if (self) {
        _borderWidth = 2.0;
        _borderColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
        _backgroundColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:0.1];
        _fillColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    }
    return self;
}

- (void)dealloc {
    TTRelease(_borderColor);
    TTRelease(_backgroundColor);
    TTRelease(_fillColor);
    [super dealloc];
}



#pragma mark - UIView

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGRect circleRect = CGRectInset(self.bounds, 2, 2);
	
    CGContextRef context = UIGraphicsGetCurrentContext();
	
    // Draw background
    CGContextSetLineWidth(context, _borderWidth);
    CGContextSetStrokeColorWithColor(context, _borderColor.CGColor);
    CGContextSetFillColorWithColor(context, _backgroundColor.CGColor);
    
    CGContextFillEllipseInRect(context, circleRect);
    CGContextStrokeEllipseInRect(context, circleRect);
	
    // Draw progress
    float x = (self.bounds.size.width / 2);
    float y = (self.bounds.size.height / 2);
    CGContextSetFillColorWithColor(context, _fillColor.CGColor);
    CGContextMoveToPoint(context, x, y);
    
    CGContextAddArc(context, // context
                    x, y, // center
                    (self.bounds.size.width - 2*_borderWidth) / 2, // radius
                    -(M_PI / 2), // startAngle
                    (self.progress * 2 * M_PI) - M_PI / 2, // endAngle
                    0); // clockwise
    
    CGContextClosePath(context);
    CGContextFillPath(context);
}

@end
